package org.dk.phonemq;

import com.alibaba.fastjson.JSONObject;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQPushConsumerLifecycleListener;
import org.apache.shiro.SecurityUtils;
import org.dk.mapper.TbOrderMapper;
import org.dk.mapper.TbUserscoreaccountMapper;
import org.dk.pojo.TbOrder;
import org.dk.pojo.TbOrderExample;
import org.dk.pojo.TbUser;
import org.dk.pojo.TbUserscoreaccount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
@RocketMQMessageListener(topic = "tx-topic",consumerGroup = "tx_message")
public class oderItemMessage implements RocketMQListener<String>, RocketMQPushConsumerLifecycleListener {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private TbOrderMapper tbOrderMapper;
    @Autowired
    private TbUserscoreaccountMapper tbUserscoreaccountMapper;

    @Override
    public void onMessage(String s) {

    }

    @Override
    public void prepareStart(DefaultMQPushConsumer defaultMQPushConsumer) {
        //MessageListenerConcurrently普通消息 MessageListenerOrderly 顺序消息
        defaultMQPushConsumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                //获取消息
                MessageExt messageExt = list.get(0);
                //重试次数
                int reconsumeTimes = messageExt.getReconsumeTimes();
                System.out.println(reconsumeTimes);
                //消息id
                String msgId = messageExt.getMsgId();
                System.out.println("消息id"+msgId);
                try {
                   // Object mq = redisTemplate.boundHashOps("mq").get(msgId);
                   // if (mq == null){
                        //主题
                        String topic = messageExt.getTopic();
                        System.out.println(topic);
                        //消息内容
                    String str = new String(messageExt.getBody(),"utf-8");

                        TbUserscoreaccount tbUserscoreaccount = new TbUserscoreaccount();
                        tbUserscoreaccount.setCreateTime(new Date());
                        tbUserscoreaccount.setUpdataTime(new Date());
                        tbUserscoreaccountMapper.insertSelective(tbUserscoreaccount);
                   /* }else{
                        System.out.println("重复消费");
                        return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                    }*/
                }catch (Exception e){
                    //重试次数大于三次直接返回
                    if(reconsumeTimes >= 3){
                        return ConsumeConcurrentlyStatus.RECONSUME_LATER;
                    }
                    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
    }
}
